// pages/user/user.ts
var { request } = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPassword:true,
    roleList: [],
    projectList: [] as any,
    roleIndex:'',
    projectIndex:'',
    projectId:'',
    role :wx.getStorageSync('role')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.initRoleList()
    this.initProjectList()    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  initRoleList(){
    request('/admin/role/list', {
    }).then((res: any) => {
      if (res.data.code == 1) {
        this.setData({
          roleList: res.data.data
        })
        console.log(this.data.roleList,'roleList');
      }
    })
  },
  initProjectList(){
    request('/admin/project/list', {
      method:'POST',
      data:{}
    }).then((res: any) => {
      if (res.data.code == 1) {
        this.setData({
          projectList: res.data.data
        })
        console.log(this.data.projectList,'projectList');
      }
    })
  },
  handleAudit(){
    wx.navigateTo({
      url: '/pages/audit/audit',
    })
  },
  handlePassword(e: any) {
    console.log(e);
    this.setData({
      isPassword: !this.data.isPassword
    })
  },
  roleChange: function(e: any) {
    console.log('role发送选择改变，携带值为', e.detail.value)
    this.setData({
      roleIndex: e.detail.value
    })
  },
  projectChange: function(e: any) {
    console.log(e.detail.value,'projectChange')
    this.setData({
      projectIndex: e.detail.value,
      projectId:this.data.projectList[e.detail.value].id
    })
  },
  addProject(){
    let that = this
    wx.showModal({
      editable:true,
      placeholderText:'输入项目名称',
      success (res) {  
        if (res.confirm) {
          console.log('用户点击确定',res)
          request('/admin/project/add', {
            method:'POST',
            data:{
              name:res.content
            }
          }).then((res: any) => {
            if(res.data.code==1){
              that.initProjectList()    
              wx.showToast({
                title: '添加成功',
                icon: 'none',
              });
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  formSubmit(e: any) {
    const formData = e.detail.value;
    if(this.data.role!=2){
      formData.processStatus=1
    }
    if (!formData.username||!formData.name||!formData.password||!formData.role||!formData.project) {
      wx.showToast({
        title: '请填写完整用户信息',
        icon: 'none',
      });
      return;
    }
    formData.project  = this.data.projectId
    console.log(formData,'formSubmit');
    request('/admin/employee/insert', {
      data: formData,
      method:'POST'
    }).then((res: any) => {
      if (res.data.code == 1) {
        wx.showToast({
          title: '创建成功',
          icon: 'success',
          duration: 2000,
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})